package com.sfac.cqlg.service.impl;

import com.sfac.cqlg.dao.Covid19Dao;
import com.sfac.cqlg.entity.Covid19;
import com.sfac.cqlg.service.Covid19Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Covid19 Service Impl
 */
@Service
public class Covid19ServiceImpl implements Covid19Service {
    @Autowired
    private Covid19Dao covid19Dao;

    @Override
    public List<Covid19> getCovid19sForProvince() {
        return Optional
                .ofNullable(covid19Dao.getCovid19sForProvince())
                .orElse(Collections.emptyList());
    }

    @Override
    public List<Covid19> getCovid19sByProvince(String province) {
        return Optional
                .ofNullable(covid19Dao.getCovid19sByProvince(province))
                .orElse(Collections.emptyList())
                .stream()
                .sorted(Comparator.comparing(Covid19 :: getStatisticalTime))
                .collect(Collectors.toList());
    }
}
